package com.wyd.component.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/07/23:32
 * @Description:
 */
@Component
public class FanoutConsumer {

  @RabbitListener(bindings =@QueueBinding(   //把队列和交换机进行绑定
      value = @Queue("fanout_queue1"),  //绑定队列
      exchange = @Exchange(name = "logs",type = ExchangeTypes.FANOUT )//绑定交换机和设置交换机的名称和类型
  )
  )
  public void receive1(String msg,Message message, Channel channel){
    long deliveryTag = message.getMessageProperties().getDeliveryTag();//消息投递ID
    try {
      channel.basicAck(deliveryTag,false);
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("消费者【1】接收到消息:"+msg);
  }

  @RabbitListener(bindings =@QueueBinding(
      value = @Queue("fanout_queue2"),
      exchange = @Exchange(name = "logs",type = ExchangeTypes.FANOUT )
  )
  )
  public void receive2(String msg,Message message, Channel channel){
    long deliveryTag = message.getMessageProperties().getDeliveryTag();//消息投递ID
    try {
      channel.basicAck(deliveryTag,false);
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("消费者【2】接收到消息:"+msg);
  }

}
